<!DOCTYPE html>
<html height100 xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta th:replace="authority/dynamic/common/edit/head"/>
    <style>
        .layui-checkbox-disbaled span {
            background-color: #efefef !important;
        }
    </style>
</head>

<body class="childrenBody" padding10>
<form class="layui-form layui-form-pane" lay-filter="form-dml-column" onsubmit="return false;">
    <input type="hidden" name="id" value="">
    <!--/*@thymesVar id="dmlId" type="java.lang.String"*/-->
    <input type="hidden" name="dmlId" th:value="${dmlId}">
    <div class="layui-row layui-col-space15 layui-form-item">
        <div class="layui-col-ms2 layui-col-md4 layui-col-lg4">
            <label for="C_name" class="layui-form-label"><span class="x-red">*</span>字段名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="C_name" name="name" lay-verify="required"
                       placeholder="字段名" autocomplete="off" value="">
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    语句中含有的字段名称
                </div>
            </div>
        </div>
        <div class="layui-col-ms2 layui-col-md4 layui-col-lg4">
            <label for="C_label" class="layui-form-label"><span class="x-red">*</span>别名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="C_label" name="label" lay-verify="required"
                       placeholder="别名" autocomplete="off" value="">
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    查询结果别名
                </div>
            </div>
        </div>
        <div class="layui-col-ms2 layui-col-md4 layui-col-lg4">
            <label for="C_caption" class="layui-form-label">中文名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="C_caption" name="caption"
                       placeholder="中文名" autocomplete="off" value="">
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    查询结果需要中文名时 会显示此值
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row layui-col-space15 layui-form-item">
        <div class="layui-col-sm6">
            <label for="C_extype1" class="layui-form-label"><span class="x-red">*</span>ORACLE 类型</label>
            <div class="layui-input-block">
                <select name="extype1" id="C_extype1" type="text" lay-verify="required" lay-search>
                    <option value="">--请选择--</option>
                </select>
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    数据所对应的ORACLE数据类型
                </div>
            </div>
        </div>
        <div class="layui-col-sm6">
            <label for="C_dataType" class="layui-form-label"><span class="x-red">*</span>JAVA 类型</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="C_dataType" name="dataType"
                       placeholder="JAVA类型（如：java.lang.String" autocomplete="off" value="">
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    字段数据对应的Java类型（全类名）
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row layui-col-space15 layui-form-item">
        <div class="layui-col-sm6">
            <label for="C_columnType" class="layui-form-label"><span class="x-red">*</span>列类型</label>
            <div class="layui-input-block">
                <select name="columnType" id="C_columnType" type="text" lay-verify="required" lay-filter="columnType">
                    <option value="">--请选择--</option>
                </select>
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    设置数据列的作用，查询语句列类型只有SELECT
                </div>
            </div>
        </div>
        <div class="layui-col-sm6">
            <label for="C_dataType" class="layui-form-label"><span class="x-red">*</span>列序号</label>
            <div class="layui-input-block">
                <input type="number" class="layui-input" id="C_columnIndex" name="columnIndex"
                       placeholder="列序号" autocomplete="off" value="">
            </div>
            <div class="my-notes">
                <div class="layui-elip layui-text content">
                    传参时特别重要，必须和语句中‘?’号所对应的顺序一样
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row layui-form-item" pane>
        <label class="layui-form-label"><span class="x-red">*</span>列属性</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-no-select zsa-attrs-any-no-select"
                   value="1" title="主键">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-no-select zsa-attrs-any-no-select"
                   value="2" title="不能为空">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-no-select zsa-attrs-any-no-select"
                   value="4" title="insert字段">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-no-select zsa-attrs-any-no-select"
                   value="8" title="update字段">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-no-select zsa-attrs-any-no-select"
                   value="16" title="只读">
            <input type="checkbox" lay-filter="checkbox_attrs" value="32" title="可查询">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-any-no-select" value="64" title="可排序">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-any-no-select" value="128" disabled
                   title="可用于分组">
            <input type="checkbox" lay-filter="checkbox_attrs" value="256" title="用于筛选">
            <input type="checkbox" lay-filter="checkbox_attrs" class="zsa-attrs-any-no-select" value="512" title="用于显示">
            <input type="checkbox" lay-filter="checkbox_attrs" value="1024" disabled title="特殊处理CODE">
        </div>
    </div>
    <div class="layui-row layui-form-item" pane>
        <label for="C_isValid" class="layui-form-label"><span class="x-red">*</span>是否有效</label>
        <div class="layui-input-block">
            <input type="checkbox" name="isValid" id="C_isValid" lay-skin="switch" checked lay-text="正常|无效">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label for="C_displayExpr" class="layui-form-label">显示表达式
            <small>(除 普通查询0 以外，都会查询字段的表达式)</small>
        </label>
        <div class="layui-input-block">
            <pre style="height: 100px;width: 100%" id="C_displayExpr" name="displayExpr"></pre>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" style="float: right" id="my-submit" lay-filter="submit" lay-submit>保存信息</button>
    </div>
</form>

<script th:replace="authority/dynamic/common/edit/footer"></script>

<script>
    var editor_displayExpr = createAce("C_displayExpr", "ace/mode/sql");

    layui.use(['form', 'lutil', 'initPage'], function () {
        var form = layui.form,
            lutil = layui.lutil,
            initPage = layui.initPage;

        var init = initPage.render({successCallback: initPageSuccess})
            .addFunction(initColumnTypes)
            .addFunction(initOracleTypes)
            .start();

        function initColumnTypes(functionSuccess) {
            ajaxRequestByCsrf(content_path + 'authority/dml_column/column_types.do', {
                dmlId: $('input[name=dmlId]').val()
            }, function (res) {
                $.each(res.data, function (k, v) {
                    $("#C_columnType").append('<option value="' + v + '">' + k + '</option>');
                });
                var keys = Object.keys(res.data);
                if (keys.length === 1) $("#C_columnType").val(res.data[keys[0]]), changeColumnType(res.data[keys[0]]), form.render('checkbox', 'form-dml-column');
                form.render('select', 'form-dml-column');
            }, null, function (res) {
                functionSuccess();
            });
        }

        function initOracleTypes(functionSuccess) {
            ajaxRequestByCsrf(content_path + 'authority/dml_column/oracle_types.do', {}, function (res) {
                $.each(res.data, function (k, v) {
                    $("#C_extype1").append('<option value="' + k + '">' + k + '</option>');
                });
                form.render('select', 'form-dml-column');
            }, null, function (res) {
                functionSuccess();
            });
        }

        form.on('select(columnType)', function (data) {
            changeColumnType(data.value);
            form.render('checkbox', 'form-dml-column');
        });

        function changeColumnType(value) {
            if (value === '0' || value === 0) {
                $('input[lay-filter="checkbox_attrs"].zsa-attrs-no-select').each(function (i, e) {
                    this.checked = false, $(this).attr("disabled", "disabled"), $(this).hide();
                });
            } else if (value === '-1' || value === -1) {
                $('input[lay-filter="checkbox_attrs"].zsa-attrs-any-no-select').each(function (i, e) {
                    this.checked = false, $(this).attr("disabled", "disabled"), $(this).hide();
                });
            } else {
                $('input[lay-filter="checkbox_attrs"].zsa-attrs-no-select').removeAttr("disabled");
                $('input[lay-filter="checkbox_attrs"].zsa-attrs-any-no-select').removeAttr("disabled");
            }
        }

        form.on('submit(submit)', function (formData) {
            var displayExpr = editor_displayExpr.getValue(), attrs = 0;
            $('input[type=checkbox][lay-filter=checkbox_attrs]').each(function (i, e) {
                if (e.checked) attrs += parseFloat(e.value);
            });
            if (!attrs || attrs === '0')
                return lutil.error_alert("请设置列属性！！"), false;
            var loding = layer.load(2, {shade: [0.3, '#eee']});
            ajaxRequestByCsrf(content_path + 'authority/dml_column/save.do', $.extend(formData.field, {
                displayExpr: displayExpr,
                isValid: formData.field['isValid'] ? 1 : 0,
                attrs: attrs
            }), function (res) {
                lutil.success_msg(res.message);
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
            }, null, function (res) {
                layer.close(loding);
            });
            return false;
        });

        function initPageSuccess() {
            if ("[[${id}]]" !== '') {
                var loding = layer.load(2, {shade: [0.3, '#eee']});
                ajaxRequestByCsrf(content_path + 'authority/dml_column/detail/[[${id}]].do', {}, function (res) {
                    form.val("form-dml-column", $.extend(res.data, {isValid: res.data['isValid'] === 1}));
                    editor_displayExpr.setValue(res.data['displayExpr'] || '');
                    $('input[type=checkbox][lay-filter=checkbox_attrs]').each(function (i, e) {
                        var dValue = parseFloat(e.value);
                        e.checked = ((res.data['attrs'] & dValue) === dValue);
                    });
                    changeColumnType(res.data['columnType']);
                    form.render('checkbox', 'form-dml-column');
                }, null, function (res) {
                    layer.close(loding);
                });
            }
        }
    })
</script>
</body>
</html>